home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 15282 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.4 KB

  1. Path: ix.netcom.com!netnews
  2. From: philma@ix.netcom.com(Phil Majtan)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: bubble sort walkthrough needed, please
  5. Date: 18 Apr 1996 00:27:12 GMT
  6. Organization: Netcom
  7. Message-ID: <4l4290$76g@cloner2.ix.netcom.com>
  8. References: <4l39b4$s2h@coranto.ucs.mun.ca>
  9. NNTP-Posting-Host: det-mi4-14.ix.netcom.com
  10. X-NETCOM-Date: Wed Apr 17  5:27:12 PM PDT 1996
  11.  
  12. In <4l39b4$s2h@coranto.ucs.mun.ca> jdeeley@calvin.stemnet.nf.ca
  13. (J.Deeley) writes: 
  14. >
  15. >Hi, everyone. I would appreciate your help on this problem that I am
  16. >sure you could code in your sleep.
  17. >
  18. >I am trying to understand how this bubble sort is working. I
  19. understand
  20. >the theory, and I understand how the whole thing works except for the
  21. >few lines under the comment /* now sort them using a bubble sort */
  22. >In other words, it's only the lines 
  23. >
  24. >for(a=1; a<count; a++)
  25. >                for(b=count-1; b>=a;--b) 
  26. >
  27. >that I can't fathom. I've been working at this ever since last night
  28. and
  29. >I still don't get it. Could someone tell me what is going on in that
  30. >part?
  31. >
  32.  
  33. It's easy to understand if you imagine a column of numbers and picture
  34. the pointers alongside.  The a starts at the top and goes down to one
  35. less than the bottom.  The b starts at the bottom and goes up to one
  36. less than the a.  The b pointer compares to the a and if the value
  37. matches what we're looking for (greater) then the two numbers are
  38. swapped.  In effect you are "bubbling" the numbers up to the top.
  39. Easy
  40. Phil
  41.  
  42.